package net.huashitong.scheduling.web;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sirdc.modules.core.filter.Paging;
import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.utils.ObjectUtils;

import net.huashitong.scheduling.entity.AreaRanking;

import net.huashitong.scheduling.entity.TownshipMotivate;
import net.huashitong.scheduling.filter.AreaRankingFilter;
import net.huashitong.scheduling.service.AreaRankingService;
import net.huashitong.scheduling.service.TownshipMotivateService;
@Controller
@RequestMapping("/app/areaRanking/")
public class AreaRankingController extends JsonBaseController {
	 @Autowired
	 private AreaRankingService areaRankingService;
	 @Autowired
	 private TownshipMotivateService townshipMotivateService;
	 public static final String[] years={"2022", "2021","2020","2020","2019","2018","2017","2016"};
	 public static final String[] months = {"12","09","06","03"};
	 @Override
	 protected String getView(String arg0) {
			// TODO Auto-generated method stub
			return null;
	} 
	@ResponseBody
	@RequestMapping("/queryAreaRankingList")
	public Message  queryAreaRankingList(@RequestBody AreaRankingFilter filter){
		 Map<String, Object> map = new HashMap<String, Object>();
		 List<Map<String, Object>> lists = new ArrayList<Map<String,Object>>();
		 Map<String, Object> map2 = new HashMap<String, Object>();
		 List<Map<String, Object>> lists2 = new ArrayList<Map<String,Object>>();
		 Map<String, Object> map3 = new HashMap<String, Object>();
		 List<Map<String, Object>> lists3 = new ArrayList<Map<String,Object>>();
		 Map<String, Object> map4 = new HashMap<String, Object>();
		 List<Map<String, Object>> lists4 = new ArrayList<Map<String,Object>>();
		 String year="";
		  if("2".equals(filter.getAreaId())){
			  List<AreaRanking> arList2=new ArrayList<AreaRanking>();
			 if(ObjectUtils.isBlank(filter.getYear())){
				 for(int j=0;j<AreaRankingController.years.length;j++){
					 filter.setYear(years[j]);
					 arList2=areaRankingService.queryAreaRankingList(filter);
					 if(ObjectUtils.isNotBlank(arList2)){
						 year=years[j];
						 break;
					 }
				 }
			 }else{
				 year=filter.getYear();
				 arList2=areaRankingService.queryAreaRankingList(filter);
			 }
			if(ObjectUtils.isNotBlank(arList2)){
				 map.put("total", arList2.size());
			 for(AreaRanking ar2:arList2){
				 if("蕉城区".equals(ar2.getArea())){
					 map.put("industryId", ar2.getIndustryId());
					 map.put("industryName", ar2.getIndustryName());
					 map.put("area", ar2.getArea());
					 map.put("cumulative", ar2.getCumulative());
					 map.put("ratio", ar2.getRatio());
					 map.put("cumulativeRanking",""+ar2.getCumulativeRanking());
					 map.put("ratioRanking",""+ar2.getRatioRanking());
					 map.put("unit", ar2.getUnit());
					
			 }
		   }
		  }
		  }
		 if(!"-1".equals(filter.getCurrentPage())){
			  Paging p=new Paging();
			 p.setCurrentPage(Integer.valueOf(filter.getCurrentPage()));
			filter.setPaging(p);
		 }
		 List<AreaRanking> arList=new ArrayList<AreaRanking>();
		 if(ObjectUtils.isBlank(filter.getYear())){
			 for(int j=0;j<AreaRankingController.years.length;j++){
			 filter.setYear(years[j]);
			 arList=areaRankingService.queryAreaRankingList(filter);
			 if(ObjectUtils.isNotBlank(arList)){
				 year=years[j];
				 break;
			 }
			}
		 }else{
			  arList=areaRankingService.queryAreaRankingList(filter); 
			  year=filter.getYear();
		 }
		
		 DecimalFormat df=new DecimalFormat("##.##");
		 Integer zong=1;
		 if(ObjectUtils.isNotBlank(arList)){
		 for(AreaRanking ar:arList){
			 if("A08".equals(filter.getIndustryId())||"A14".equals(filter.getIndustryId())){
				if(ObjectUtils.isNotBlank(ar.getCumulative())) {
					 String st2=df.format(Double.parseDouble(ar.getCumulative())/10000);
					   ar.setCumulative(st2);
				}
			 }
			 if("蕉城区".equals(ar.getArea())||("宁德市".equals(ar.getArea())&&"0".equals(filter.getAreaId()))){
				 if(!"2".equals(filter.getAreaId())){
				 map.put("industryId", ar.getIndustryId());
				 map.put("industryName", ar.getIndustryName());
				 map.put("area", ar.getArea());
				 map.put("cumulative",ar.getCumulative());
				 map.put("ratio", ar.getRatio());
				 if("1".equals(filter.getAreaId())){
					 map.put("cumulativeRanking",""+ar.getCumulativeRanking());
					 map.put("ratioRanking",""+ar.getRatioRanking());
				 }else if("0".equals(filter.getAreaId())){
					 map.put("cumulativeRanking",""+ar.getCumulativeRanking());
					 map.put("ratioRanking",""+ar.getRatioRanking());
				 }else if("3".equals(filter.getAreaId())){
					 map.put("cumulativeRanking",""+ar.getCumulativeRanking());
					 map.put("ratioRanking",""+ar.getRatioRanking());
				 }else if("2".equals(filter.getAreaId())){
					 map.put("cumulativeRanking",""+ar.getCumulativeRanking());
					 map.put("ratioRanking",""+ar.getRatioRanking());
				 }
				// map.put("cumulativeRanking",0);
				 map.put("unit", ar.getUnit());
				 }              
			 }
				 Map<String, Object> map5 = new HashMap<String, Object>();
				 if("0".equals(filter.getAreaId())){
					 map5.put("industryId", ar.getIndustryId());
					 map5.put("industryName", ar.getIndustryName());
					 map5.put("area", ar.getArea());
					 map5.put("cumulative", ar.getCumulative());
					 map5.put("ratio", ar.getRatio());
					 map5.put("unit", ar.getUnit());
					 map5.put("cumulativeRanking",""+zong);
					 map5.put("ratioRanking",""+zong);
					 if("福建省".equals(ar.getArea())){
						 map5.put("state","-1");
						 if("".equals(ar.getCumulative())){
							 map5.put("cumulative","--");
						 }
						 lists2.add(0,map5);
					 }else{
						 map5.put("state", filter.getState()); 
						 lists2.add(map5);
					 }
					
				 }else if("1".equals(filter.getAreaId())){
					 map5.put("industryId", ar.getIndustryId());
					 map5.put("industryName", ar.getIndustryName());
					 map5.put("area", ar.getArea());
					 map5.put("cumulative",ar.getCumulative());
					 map5.put("ratio", ar.getRatio());
				      map5.put("cumulativeRanking",""+ar.getCumulativeRanking());
					 map5.put("ratioRanking",""+ar.getRatioRanking());
					/* map5.put("cumulativeRanking", zong);
					 map5.put("ratioRanking", zong);*/
					 map5.put("state", filter.getState());
					 map5.put("unit", ar.getUnit());
					 if(zong<11){
						 lists2.add(map5);
					 }else if(zong<17&&zong>10){
						 lists3.add(map5);
					 }else{
						 lists4.add(map5);
					 }
				 }else if("2".equals(filter.getAreaId())){
					 map5.put("industryId", ar.getIndustryId());
					 map5.put("industryName", ar.getIndustryName());
					 map5.put("area", ar.getArea());
					 map5.put("cumulative", ar.getCumulative());
					 map5.put("ratio", ar.getRatio());
					 map5.put("cumulativeRanking",""+ar.getCumulativeRanking());
					 map5.put("ratioRanking", ""+ar.getRatioRanking());
					 map5.put("state", filter.getState());
					 map5.put("unit", ar.getUnit());
					 lists2.add(map5);
				 }else if("3".equals(filter.getAreaId())){
					 map5.put("industryId", ar.getIndustryId());
					 map5.put("industryName", ar.getIndustryName());
					 map5.put("area", ar.getArea());
					 map5.put("cumulative", ar.getCumulative());
					 map5.put("ratio", ar.getRatio());
					 map5.put("unit", ar.getUnit());
					 map5.put("cumulativeRanking",""+ar.getCumulativeRanking());
					 map5.put("ratioRanking", ""+ar.getRatioRanking());
					 if("宁德市".equals(ar.getArea())){
						 map5.put("state","-1");
						 lists2.add(0,map5);
					 }else{
						 map5.put("state", filter.getState()); 
						 lists2.add(map5);
					 }
					
				
			 }
			 
			 if(!"福建省".equals(ar.getArea())){
				
				 zong++;
			 }
			
		 }	
		}
		 if("1".equals(filter.getAreaId())){
			 map2.put("title", "排名前十各市/区"); 
			 map3.put("title", "排名在第11-16各市/区");
			 map4.put("title", "排名末十市/区");
			 map2.put("data", lists2);
			 map3.put("data", lists3);
			 map4.put("data", lists4);
			 lists.add(map2);
			 lists.add(map3);
			 lists.add(map4);
		 }else if("0".equals(filter.getAreaId())){
			 map2.put("data", lists2);
			 lists.add(map2);
		 }else if("3".equals(filter.getAreaId())){
			 map2.put("data", lists2);
			 lists.add(map2);
		 }else if("2".equals(filter.getAreaId())){
			 map2.put("data", lists2);
			 lists.add(map2);
		 }
		  map.put("lists", lists);
		  map.put("date",year+"年度");
		 return coverMessage("200","数据获取成功",map);
	}
	@ResponseBody
	@RequestMapping("/queryIndustryName")
	public Message queryIndustryName(@RequestBody AreaRankingFilter filter){
		List<Map<String, Object>> lists = new ArrayList<Map<String,Object>>();
		 List<AreaRanking> arList=new ArrayList<AreaRanking>();
		if(ObjectUtils.isBlank(filter.getYear())){
			 for(int j=0;j<AreaRankingController.years.length;j++){
				 filter.setYear(years[j]);
				 arList=areaRankingService.queryIndustryName(filter);
				 if(ObjectUtils.isNotBlank(arList)){
					 break;
				 }
			 }
		 }else{
			 arList=areaRankingService.queryIndustryName(filter);
		 }
	   if(ObjectUtils.isNotBlank(arList)){
	     for(AreaRanking ar:arList){
			 Map<String, Object> map = new HashMap<String, Object>();
			 map.put("industryId", ar.getIndustryId());
			 map.put("industryName",ar.getIndustryName());
			 lists.add(map);
		 }
	   }
		return coverMessage("200","数据获取成功",lists);
	}
	
	@ResponseBody
	@RequestMapping("/TownshipMotivateList")
	public Message queryTownshipMotivateList(@RequestBody AreaRankingFilter filter){
		 List<TownshipMotivate> tList=new ArrayList<TownshipMotivate>();
		int ss=0;
		String date="";
		if(ObjectUtils.isBlank(filter.getDate())){
			 for(int j=0;j<AreaRankingController.years.length;j++){
				 if(ss==1){
					 break;
				 }
				 
				 for(int i=0; i<AreaRankingController.months.length;i++){
					  tList= townshipMotivateService.queryTownshipMotivateByDate(years[j]+months[i]);
					 if(ObjectUtils.isNotBlank(tList)){
						 ss=1;
						 date=years[j]+months[i];
						 break;
					 }
				 }
			 }
		}else{
			  tList= townshipMotivateService.queryTownshipMotivateByDate(filter.getDate());
			  date=filter.getDate();
		}
		 Map<String, Object> map = new HashMap<String, Object>();
		 map.put("lists", tList);
		 map.put("date", date);
		return coverMessage("200","数据获取成功",map);
	}
	
}
